package com.kksoft.android.xiaoliaobaike.fetcher;

import java.util.concurrent.ExecutorService;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.kksoft.android.xiaoliaobaike.Const;
import com.kksoft.android.xiaoliaobaike.fetcher.thread.FetchSeriesPageRangeThread;
import com.kksoft.android.xiaoliaobaike.fetcher.thread.FetchSeriesThread;

/**
 * 解析Series
 * 
 * @author KK
 */
@Deprecated
public class SeriesFetcher {
	public static final String TAG = "SeriesFetcher";

	Handler seriesHandler;
	ExecutorService executor;

	public SeriesFetcher(ExecutorService executor, Handler seriesHandler) {
		this.seriesHandler = seriesHandler;
		this.executor = executor;
	}

	public void fetch(int page) {
		fetch(page, page);
	}

	public void fetchAll() {
		// 获得页码范围
		Handler pageHandler = new Handler() {
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case Const.MSG_SERIES_PAGE_RANGE:
					int pageStart = msg.arg1;
					int pageEnd = msg.arg2;
					if ((pageStart == 0 || pageEnd == 0) || pageStart > pageEnd) {

					} else {
						for (int page = pageStart; page <= pageEnd; page++) {
							fetch(page);
						}
					}
					break;
				}
			}
		};
		executor.execute(new FetchSeriesPageRangeThread(pageHandler));
	}

	public void fetch(int fromPage, int toPage) {
		Log.d(TAG, "add series thread to ExecutorService : from page " + fromPage + " to page " + toPage);
		executor.execute(new FetchSeriesThread(seriesHandler, null));
	}

	public void fetchPageRange() {
		new Thread(new FetchSeriesPageRangeThread(seriesHandler)).start();
	}

}